import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';

class StatementController extends BaseController {
  RichTextLinkType linkType = RichTextLinkType.none;

  Map data = {};

  String titleText = '';

  var content = ''.obs;

  var version = ''.obs;

  var userMsgList = ''.obs;

  var aboutUs = ''.obs;

  var shareList = ''.obs;

  var serviceTerm = ''.obs;

  var privacyPolicy = ''.obs;

  var presalePolicy = ''.obs;

  var appVersion = ''.obs;

  final webViewCtr = WebViewController();

  @override
  void onInit() {
    super.onInit();
    linkType = Get.arguments as RichTextLinkType;
    titleText = getLinkTypeText();
    getStoreConfig();
    _loadAppVersion();
    update();
  }

  // app版本号
  Future<void> _loadAppVersion() async {
    try {
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      appVersion.value = '${packageInfo.version}  (${packageInfo.buildNumber})';
    } catch (e) {
      appVersion.value = 'Unknown Version'; // 异常情况下的默认值
    }
  }

  ///获取隐私协议和用户协议
  getStoreConfig() {
    Tools.showLoading();
    Network.getStoreConfig().then((value) {
      data = value;
      RichTextLinkType linkType = Get.arguments as RichTextLinkType;
      handleLinkType(linkType);
      titleText = getLinkTypeText();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  getAppInfo() {
    Tools.showLoading();
    Network.getAppInfo().then((v) {
      version.value = v.version.toString();
      Tools.hideLoading();
      update();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  @override
  void onClose() {
    super.onClose();
  }

  void handleLinkType(RichTextLinkType linkType) {
    switch (linkType) {
      case RichTextLinkType.none:
        logger.d("没有链接");
        break;

      case RichTextLinkType.aboutUs:
        logger.d("关于我们");
        aboutUs.value = data['aboutUs'];
        break;

      case RichTextLinkType.shareList:
        logger.d("第三方共享清单");
        shareList.value = data['shareList'];
        break;

      case RichTextLinkType.userMsgList:
        logger.d("个人信息清单");
        userMsgList.value = data['shareList'];
        break;

      case RichTextLinkType.presaleNotes:
        logger.d("预售规则");
        break;

      case RichTextLinkType.presalePolicy:
        presalePolicy.value = data['presalePolicy'];
        logger.d("预售协议");
        break;

      case RichTextLinkType.privacyPolicy:
        logger.d("隐私协议");
        privacyPolicy.value = data['privacyPolicy'];
        break;

      case RichTextLinkType.purchaseNotes:
        logger.d("购买须知");
        break;

      case RichTextLinkType.raffleNotes:
        logger.d("抽奖规则");
        break;

      case RichTextLinkType.serviceTerm:
        serviceTerm.value = data['serviceTerm'];
        logger.d("服务条款");
        break;

      case RichTextLinkType.signInNotes:
        logger.d("签到规则");
        break;

      default:
        logger.d("未知的链接类型");
    }
  }

  String getLinkTypeText() {
    RichTextLinkType linkType = Get.arguments as RichTextLinkType;
    switch (linkType) {
      case RichTextLinkType.none:
        return "没有链接";

      case RichTextLinkType.aboutUs:
        return "关于我们";

      case RichTextLinkType.shareList:
        return "第三方共享清单";

      case RichTextLinkType.userMsgList:
        return "个人信息清单";

      case RichTextLinkType.presaleNotes:
        return "预售规则";

      case RichTextLinkType.presalePolicy:
        return "预售协议";

      case RichTextLinkType.privacyPolicy:
        return "隐私协议";

      case RichTextLinkType.purchaseNotes:
        return "购买须知";

      case RichTextLinkType.raffleNotes:
        return "抽奖规则";

      case RichTextLinkType.serviceTerm:
        return "服务条款";

      case RichTextLinkType.signInNotes:
        return "签到规则";

      default:
        return "未知的链接类型";
    }
  }
}

enum RichTextLinkType {
  none,
  aboutUs, // 关于我们
  shareList, // 第三方共享清单
  userMsgList, // 个人信息清单
  presaleNotes, // 预售规则
  presalePolicy, // 预售协议
  privacyPolicy, // 隐私协议
  purchaseNotes, // 购买须知
  raffleNotes, // 抽奖规则
  serviceTerm, // 服务条款
  signInNotes, // 签到规则
}
